﻿#define _CRT_SECURE_NO_WARNINGS

#include "AVLTree.h"
#include <vector>

//测试代码

void TestAVLTree1()
{
    TL::AVLTree<int, int> t;
    //常规的测试⽤例
    //int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
    
    //特殊的带有双旋场景的测试⽤例
    int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };

    for (auto e : a)
    {
        t.insert({ e, e });
    }
    for (auto e : a)
    {
        t.erase(e);
        t.IsBalance();
    }
    /*t.erase(4);
    t.erase(5);
    t.erase(14);
    t.erase(7);*/


    t.InOrder();
    cout << t.IsBalance() << endl;
    cout << t.size() << endl;
    cout << t.height() << endl;
}

void TestAVLTree2()
{
    const int N = 10000;
    vector<int> v;
    v.reserve(N);
    srand((unsigned int)time(NULL));
    for (int i = 0; i < N; i++)
    {
        v.push_back(rand() + i);
    }
    size_t begin2 = clock();
    TL::AVLTree<int, int> t;
    for (auto e : v)
    {
        t.insert(pair<int, int>(e, e));
    }
    size_t end2 = clock();
    cout << "insert:" << end2 - begin2 << endl;
    cout << t.IsBalance() << endl;
    cout << "height:" << t.height() << endl;
    cout << "size:" << t.size() << endl;
    size_t begin1 = clock();
    // 确定在的值

    //随机值
    for (int i = 0; i < N; i++)
    {
        t.find((rand() + i));
    }
    size_t end1 = clock();
    cout << "find:" << end1 - begin1 << endl;

    size_t begin3 = clock();
    /*for (int i = 0; i < N; i++)
    {
        t.erase(i);
        bool ret = t.IsBalance();
        if (!ret)
        {
            cout << "剩余大小" << t.size() << endl;
            break;
        }
    }*/
    for (auto e : v)
    {
        t.erase(e);
        bool ret = t.IsBalance();
        if (!ret)
        {
            cout << "剩余大小" << t.size() << endl;
        }
    }
    size_t end3 = clock();
    cout << "erase:" << end3 - begin3 << endl;
}

void TestAVLTree3()
{
    vector<int> v = { 5136, 6406, 14585, 9309, 14713, 2448, 27535, 3461, 21446, 12505 };
    TL::AVLTree<int, int> t;
    for (auto e : v)
    {
        t.insert({e,e});
    }
    t.IsBalance();
    t.InOrder();
    for (auto e : v)
    {
        t.erase(e);
        t.IsBalance();

    }
    t.InOrder();

}

int main()
{
    srand((unsigned int)time(NULL));
    //TestAVLTree1();
    TestAVLTree2();
	return 0;
}